import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
// import 'package:flutter_app/helper.dart';
// import 'package:flutter_app/services/api.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/helper.dart';
import 'package:yp/services/api.dart';

import 'state.dart';

class AddInvoiceHeadLogic extends GetxController {
  final state = AddInvoiceHeadState();
  bool isSubmit = false;
  @override
  void onInit() {
    super.onInit();

    state.addOrEditor = Get.parameters['indexType'];
    if (Get.parameters['indexType'] == '1') {
      state.titleStr = '添加发票抬头';
      state.whetherDefaultBool = false;
    } else {
      state.titleStr = '修改发票抬头';
      state.whetherDefaultBool = true;
    }
    state.invoiceHeadMap = Get.arguments;
    if (state.invoiceHeadMap != null) {
      state.invoiceHeaderBool =
          Get.arguments['invoice_type'].toString(); // 发票抬头类型: 1.个人 2.公司
      state.invoiceNameController =
          TextEditingController(text: state.invoiceHeadMap['invoice_title']);
      state.dutyParagraphController = TextEditingController(
          text: state.invoiceHeadMap['consumer_tax_account']);
      state.businessAddressController =
          TextEditingController(text: state.invoiceHeadMap['consumer_addr']);
      state.enterpriseTelephoneController = TextEditingController(
          text: state.invoiceHeadMap['consumer_telephone']);
      state.bankOfDepositController = TextEditingController(
          text: state.invoiceHeadMap['consumer_bank_name']);
      state.bankAccountController = TextEditingController(
          text: state.invoiceHeadMap['consumer_bank_account']);
      state.invoiceHeaderBool = state.invoiceHeadMap['invoice_type']
          .toString(); // 发票抬头类型: 1.个人   2.公司
      state.whetherDefaultBool = state.invoiceHeadMap['default_sign'] == 1
          ? true
          : false; // 是否默认: 0.否 1.是  state.whetherDefaultBool ? 1 : 0, // 是否默认: 0.否 1.是
    } else {
      state.invoiceNameController = TextEditingController(text: '');
      state.dutyParagraphController = TextEditingController(text: '');
      state.businessAddressController = TextEditingController(text: '');
      state.enterpriseTelephoneController = TextEditingController(text: '');
      state.bankOfDepositController = TextEditingController(text: '');
      state.bankAccountController = TextEditingController(text: '');
    }

    update();
  }

  @override
  void onReady() {
    super.onReady();
  }

  void addOrEditorClick() {
    if (isSubmit) return;
    if (state.invoiceNameController.text == '') {
      toast('请输入发票名称');
      return;
    }
    if (state.invoiceHeaderBool == '2') {
      if (state.dutyParagraphController.text == '') {
        toast('请输入税号');
        return;
      }
      if (state.businessAddressController.text == '') {
        toast('请输入企业地址');
        return;
      }
      if (state.enterpriseTelephoneController.text == '') {
        toast('请输入企业电话');
        return;
      }
      if (state.bankOfDepositController.text == '') {
        toast('请输入开户银行');
        return;
      }
      if (state.bankAccountController.text == '') {
        toast('请输入银行账号');
        return;
      }
    }
    isSubmit = true;
    EasyLoading.show();
    var params = {
      'operate_type': state.invoiceHeadMap != null ? 2 : 1, // 操作类型：1.新增 2.编辑
      'access_token': SpUtil.getString('token'),
      'usr_num_id': SpUtil.getInt('empeNumId'),
      'invoice_title_info': {
        'invoice_type': state.invoiceHeaderBool, // 发票抬头类型: 1.个人 2.公司
        'invoice_title': state.invoiceNameController.text,
        'consumer_tax_account': state.dutyParagraphController.text,
        'consumer_bank_name': state.bankOfDepositController.text,
        'consumer_bank_account': state.bankAccountController.text,
        'consumer_addr': state.businessAddressController.text,
        'consumer_telephone': state.enterpriseTelephoneController.text,
        'default_sign': state.whetherDefaultBool ? 1 : 0, // 更新是否默认: 0.否 1.是
      },
    };
    if (state.invoiceHeadMap != null) {
      Map<String, dynamic> invoiceTitleInfoMap = params['invoice_title_info'];
      invoiceTitleInfoMap['invoice_title_num_id'] =
          state.invoiceHeadMap['invoice_title_num_id'];
      params['invoice_title_info'] = invoiceTitleInfoMap;
    }
    print('===========   params = $params');
    Api.addInvoiceHeader(params).listen((event) {
      print('添加或者编辑 抬头发票  =========  event = $event');
      EasyLoading.dismiss();
      if (event['code'] == 0) {
        toast('保存成功');
        Get.back();
      } else {
        isSubmit = false;
        toast('保存失败');
      }
    }, onError: (error) {
      isSubmit = false;
      print('添加或者编辑 抬头发票  =========  失败  error = $error');
    });
  }
}
